package com.zhongkai.web.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.log4j.Logger;

import com.zhongkai.dao.TagDao;
import com.zhongkai.tools.CheckWord;
import com.zhongkai.tools.SpringUtils;

public class ViewTag extends BodyTagSupport {
	Logger log = Logger.getLogger(this.getClass());
	private String table;
	private String name = "row";	
	private String id;
	private String entityStringId;
	@Override
	public int doEndTag() {
		try {
			if (table == null || table.matches("\\s*") ||  CheckWord.hasInvalidWord(table))
				throw new RuntimeException("对像名不存在!");	
			if (id == null || !id.matches("\\d+"))
				throw new RuntimeException("非法的ID传入!");	
			TagDao tagDao = (TagDao) SpringUtils.getBean("tagDao",
					this.pageContext.getServletContext());
			Object o;
			if("true".equals(entityStringId)){
				o=tagDao.selectById(table, id);
			}else{
				o=tagDao.selectById(table, Integer.valueOf(id));
			}			
			pageContext.setAttribute(name, o);
			return EVAL_PAGE;
		} catch (Exception ex) {
			try {
				ex.printStackTrace();
				String path=((HttpServletRequest)pageContext.getRequest()).getContextPath();
				log.error(ex.getMessage());
				((HttpServletResponse) pageContext.getResponse())
						.sendRedirect(path+"/errorpage.jsp?errorinfo="
								+ java.net.URLEncoder.encode(ex.toString(),
										"UTF-8"));
			} catch (java.io.IOException ioe) {
				log.error(ioe.getMessage());
			}
			return EVAL_PAGE;
		}
	}
	public String getTable() {
		return table;
	}
	public void setTable(String table) {
		this.table = table;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String getId() {
		return id;
	}
	@Override
	public void setId(String id) {
		this.id = id;
	}
	public String getEntityStringId() {
		return entityStringId;
	}
	public void setEntityStringId(String entityStringId) {
		this.entityStringId = entityStringId;
	}
	
	
	
}
